<?php

class Setup_creative extends Controller
{


    function Setup_creative()
    {
        parent::Controller();
        //$this->output->enable_profiler(TRUE);
        $this->load->model('setup_creative_model');
        $this->load->helper('form');
    }

    function index()
    {
        if ( $this->auth->logged_in() )
        {
            $data['tags'] = $this->setup_creative_model->get_tags();
            $data['categories'] = $this->setup_creative_model->get_categories();
            $header['js'] = array('setup_creative.js');
            $this->template->build('setup_creative/main',$data, $header);
        }
        else
        {
            redirect('users/login');
        }
    }

	/**
	 * Displays the statistics
	 */
    function performance()
    {
        if ( $this->auth->logged_in() )
        {
			// @TODO Add statistics
            $this->template->build('setup_creative/performance');
        }
        else
        {
            redirect('users/login');
        }
    }

    function view_creative()
    {
        $creative_name = $this->input->post('creative_name');
        //$creative_name = str_replace('_', ' ', $creative_name);
        $tags = $this->input->post('tags');
        $tags_serialized = serialize($tags);

        $db_input = array(
                'name' => $creative_name,
                'tag_ids' => $tags_serialized
        );
        $this->db->insert('creative', $db_input);
        $insert_id = $this->db->insert_id();

        $page_data['creative_name'] = $creative_name;
        $tags_string = "";
        foreach($tags as $t)
        {
            $tags_string .= $t . "-";
        }
        $tags_string = substr($tags_string,0,-1);
        $page_data['tags'] = $tags_string;
        $page_data['creative_id'] = $insert_id;
        $this->template->build('setup_creative/view_creative',$page_data);
    }

	/**
	 * Adds a category
	 */
    function add_category()
    {
        $name = $this->uri->segment(3);
        $name = str_replace('_', ' ', $name);

        $data = array(
                'name' => $name
        );

        $this->db->insert('categories', $data);

        echo $this->db->insert_id();
    }

	/**
	 * Adds a tag
	 */
    function add_tag()
    {
        $tag_name = $this->uri->segment(3);
        $cat_id = $this->uri->segment(4);
        $tag_name = str_replace('_', ' ',$tag_name);

        $data = array(
                'name' => $tag_name,
                'cat_id' => $cat_id
        );

        $this->db->insert('tags', $data);
    }

    function refresh_tags_div()
    {
        $tags = $this->setup_creative_model->get_tags();
        foreach($tags as $category => $tag_array)
        {
            echo "<h3>" . $category . "</h3>";
            for($i=0;$i<count($tag_array);$i++)
            {
                $tag_values = explode('|', $tag_array[$i]);
                $tag_id = $tag_values[0];
                $tag_name = $tag_values[1];

                $data = array(
                        'name'        => 'tags[]',
                        'id'          => 'tags_checkbox',
                        'value'       => $tag_id,
                        'checked'     => FALSE,
                        'style'       => 'margin:10px',
                );

                echo form_checkbox($data);
                echo '<label>' . $tag_name . '</label><br />';
            }
        }
    }

    function refresh_cat_dropdown_div()
    {
        $categories = $this->setup_creative_model->get_categories();
        $append = 'id="selected_category"';
        echo form_dropdown('categories', $categories, $this->uri->segment(3),$append);
    }
}

/* End of file setup_creative.php */
/* Location: ./system/application/controllers/setup_creative.php */